package com.home.web.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.home.web.gwt.shared.Contact;

import java.util.Comparator;
import java.util.List;

/**
 * User: volyn
 * Date: 29.01.12
 * Time: 12:34
 */
public class Example implements EntryPoint {
    public void onModuleLoad() {
        Button button1 = new Button("Button1");
       // RootPanel.get("button1").add(button1);
        // Create a CellTable.
        CellTable<Contact> table = new CellTable<Contact>();
        // Create name columns.
        TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.getName();
            }
        };
        nameColumn.setSortable(true);
        // Create companyName column.
        TextColumn<Contact> companyNameColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.getCompanyName();
            }
        };
        companyNameColumn.setSortable(true);
        //Create phone
        TextColumn<Contact>  phoneColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.getPhone();
            }
        };
        phoneColumn.setSortable(true);
        TextColumn<Contact> emailColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.getEmail();
            }
        };
        emailColumn.setSortable(true);
        table.addColumn(nameColumn, "Name");
        table.addColumn(companyNameColumn, "Company Name");
        table.addColumn(phoneColumn, "Phone");
        table.addColumn(emailColumn, "Email");

        // Create a data provider.
        final ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();
        GenService.App.getInstance().getContactList(new AsyncCallback<List<Contact>>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Fail getContactList"+caught.toString());
            }

            @Override
            public void onSuccess(List<Contact> result) {
                for (Contact contact : result)
                    dataProvider.getList().add(contact);
            }
        });

        //SortHandler
        ColumnSortEvent.ListHandler<Contact> columnSortHandler = new ColumnSortEvent.ListHandler<Contact>(dataProvider.getList());
        columnSortHandler.setComparator(nameColumn, new Comparator<Contact>() {
            @Override
            public int compare(Contact o1, Contact o2) {
                if(o1 == o2){
                    return 0;
                }
                if(o1 != null){
                    return (o2 != null)? o1.getName().compareTo(o2.getName()):1;
                }
                return -1;
            }
        });
        table.addColumnSortHandler(columnSortHandler);
        table.getColumnSortList().push(nameColumn);
        RootPanel.get().add(table);

        button1.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                GenService.App.getInstance().generate(new AsyncCallback<Integer>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Fail" + caught.toString());
                    }

                    @Override
                    public void onSuccess(Integer result) {
                        Window.alert("" + result);
                    }
                });
            }
        });
    }
}
